package View.Student;

import Service.DormService;
import Service.ServiceImpl.DormServiceImpl;
import Service.ServiceImpl.StudentServiceImpl;
import Service.StudentService;
import View.Index.Index_admin;
import domain.Dorm;
import domain.Student;
import domain.User;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

public class UpdateStudent extends JFrame {

    public UpdateStudent(User user){
        init();
        componentInit(user);
    }

    int cnt;
    public void init(){
        this.setTitle("修改学生信息");
        this.setSize(600,446);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user){
        //------------------JLabel--------------------
        JLabel label1= new JLabel();
        label1.setText("请输入学号:");
        label1.setBounds(118,19,72,28);
        JLabel label2= new JLabel();
        label2.setText("查询结果如下:");
        label2.setBounds(179,60,179,55);
        JLabel label3= new JLabel();
        label3.setText("学号:");
        label3.setBounds(161,129,65,30);
        JLabel label4= new JLabel();
        label4.setText("姓名:");
        label4.setBounds(156,175,65,31);
        JLabel label6= new JLabel();
        label6.setText("性别：");
        label6.setBounds(157,225,65,30);
        JLabel label7= new JLabel();
        label7.setText("班级:");
        label7.setBounds(157,270,65,30);
        JLabel label8= new JLabel();
        label8.setText("宿舍号:");
        label8.setBounds(158,313,65,30);

        //------------------JTextField--------------------
        JTextField field1= new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(203,19,141,30);
        JTextField field2= new JTextField();
        field2.setEnabled(false);
        field2.setText("");
        field2.setEnabled(false);
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(234,128,120,30);
        JTextField field3= new JTextField();
        field3.setText("");
        field3.setBorder(new LineBorder(Color.lightGray));
        field3.setBounds(234,174,120,30);
        String[] item = new String[3];
        item[0]="请选择性别";
        item[1]="男";
        item[2]="女";
        JComboBox comboBox1= new JComboBox(item);
        comboBox1.setBounds(231,226,120,29);
        JTextField field5= new JTextField();
        field5.setText("");
        field5.setBorder(new LineBorder(Color.lightGray));
        field5.setBounds(232,271,120,30);

        DormService service = new DormServiceImpl();
        List<Dorm> dorms = service.dormList();
        Vector<String> item2= new Vector<>();
        item2.add("请选择你的宿舍");
        for (int i = 0; i < dorms.size(); i++) {
            Dorm dorm = dorms.get(i);
            item2.add(dorm.getDormnum());
        }

        JComboBox comboBox2= new JComboBox(item2);
        comboBox2.setBounds(233,313,120,30);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("查找");
        button1.setBounds(393,21,95,27);
        JButton button2= new JButton();
        button2.setText("修改");
        button2.setEnabled(false);
        button2.setBounds(133,364,95,27);
        JButton button3= new JButton();
        button3.setText("取消");
        button3.setBounds(312,363,95,27);





        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(label6);
        this.add(label7);
        this.add(label8);
        this.add(field1);
        this.add(field2);
        this.add(field3);
        this.add(comboBox1);
        this.add(field5);
        this.add(comboBox2);
        this.add(button1);
        this.add(button2);
        this.add(button3);


        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                StudentService service = new StudentServiceImpl();
                List<Student> students = service.findStudentByStunum(field1.getText());
                Student student = students.get(0);
                field2.setText(student.getStunum());
                field3.setText(student.getName());
                if(student.getSex().equals("男")){
                    comboBox1.setSelectedIndex(1);
                }else if(student.getSex().equals("女")){
                    comboBox1.setSelectedIndex(2);
                }
                field5.setText(student.getClasses());
                cnt=0;
                for (int i = 0; i < item2.size(); i++) {
                    if(student.getDormnum().equals(item2.get(i))){
                        cnt=i;
                        break;
                    }
                }
                comboBox2.setSelectedIndex(cnt);
                JOptionPane.showMessageDialog(new JFrame(),"查询结果如下");
                button2.setEnabled(true);
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Student student = new Student();
                student.setStunum(field1.getText());
                student.setName(field3.getText());
                student.setSex((String) comboBox1.getSelectedItem());
                student.setClasses(field5.getText());
                student.setDormnum((String) comboBox2.getSelectedItem());

                StudentService service = new StudentServiceImpl();
                service.updateStudent(student);
                JFrame frame = new JFrame();
                JOptionPane.showMessageDialog(frame,"修改成功!");
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_admin(user).setVisible(true);
            }
        });
    }
}
